#include "DirectMappedCache.h"

DirectMappedCache::DirectMappedCache(unsigned size, unsigned lineSize) :
	Cache(size, lineSize)
{
	lines = size/lineSize;
	cache = new int[lines];

	lines = size / lineSize;

	indexBits = log2(lines);

	offsetBits = log2(lineSize);

	tagBits = adressSize - indexBits - offsetBits;

	for(int i=0; i<lines; i++)
	cache[i] = -1;

	std::cout << "IndexBits = " << indexBits << std::endl;
	std::cout << "OffsetBits = " << offsetBits << std::endl;
	std::cout << "TagBits = " << tagBits << std::endl;
}

DirectMappedCache::~DirectMappedCache()
{
	delete [] cache;
}

float DirectMappedCache::getFilled()
{
	int fullCounter = 0;
	for(int i=0; i<lines; i++)
	{
		if(cache[i] != -1)
			fullCounter++;
	}
	return (float)fullCounter / lines;
}

bool DirectMappedCache::checkHit()
{
	std::cout << "cache[index] = " << cache[currentIndex] << " | txt.Tag = " << currentTag << std::endl;
	bool hit = cache[currentIndex] == currentTag;
	if(!hit)
		cache[currentIndex] = currentTag;

	return hit;
}